function [m, gr_m, info] = xt_reg(y,x,gr,gr_val)
% group mean
% y : variable to be meaned
% gr     : group of y (same length as y)
% gr_val : group values

[nobs, ny] = size(x);

ngr = length(gr_val);

gr_m = zeros(ngr,ny);
gr_n = zeros(ngr,1);

m = [];
n = [];
for i = 1:1:ngr
    
    temp_y = y(gr==gr_val(i),:);
    temp_x = x(gr==gr_val(i),:);
    temp_n = size(temp_y,1);
    
    loc_m = ( (temp_x'*temp_x)\(temp_x'*temp_y) )';
    temp_m = repmat(loc_m,temp_n,1);
    
    % group mean
    m = [m; temp_m];   %obs order
    gr_m(i,:) = loc_m; %gr order
        
    %number of obs
    n = [n; repmat(temp_n,temp_n,1)]; %obs order
    gr_n(i,:) = temp_n; %gr order
    
end

info.n = n;
info.gr_n = gr_n;
